/**
 * 
 */
package spl.army.ut;

import static org.junit.Assert.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import spl.army.Board;
import spl.army.ItemRequired;
import spl.army.Mission;

/**
 * @author Ruby
 *
 */
public class BoardTest {
	private Mission readyMission;
	private Mission firstDepends;
	private Mission secondDepends;
	private ArrayList<ItemRequired> noItemsRequireds;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		Board.resetBoard();
		this.noItemsRequireds = new ArrayList<ItemRequired>();
		ArrayList<Mission> noPreReq = new ArrayList<Mission>();		
		this.readyMission = new Mission(
							"KillBinLaden1",
							"Sniper",
							5,
							noItemsRequireds,
							noPreReq);
		Board.addMission(this.readyMission);
		this.firstDepends = new Mission(
				"KillBinLaden2",
				"Sniper",
				5,
				noItemsRequireds,
				Board.getMissionListFromString("KillBinLaden1"));
		Board.addMission(this.firstDepends);
		this.secondDepends = new Mission(
				"KillBinLaden3",
				"Sniper",
				5,
				noItemsRequireds,
				Board.getMissionListFromString("KillBinLaden1,KillBinLaden2"));
		Board.addMission(this.secondDepends);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link spl.army.Board#resetBoard()}.
	 */
	@Test
	public void testResetBoard() {
		Board.resetBoard();
		assertEquals(Board.getMissions().size(),0);
	}

	/**
	 * Test method for {@link spl.army.Board#initBoardFromPropFile(java.lang.String)}.
	 * @throws IOException 
	 * @throws ParseException 
	 * @throws FileNotFoundException 
	 */
	@Test
	public void testInitBoardFromPropFile() throws FileNotFoundException, ParseException, IOException {
		Board.resetBoard();
		Board.initBoardFromPropFile("resources/test-missions-file.txt");
		List<Mission> allMission = Board.getMissions();
		assertEquals(allMission.size(), 5);
		List<Mission> readyMission = Board.getReadyToExecuteMissions();
		assertEquals(readyMission.size(), 1);
	}

	/**
	 * Test method for {@link spl.army.Board#getReadyToExecuteMissions()}.
	 */
	@Test
	public void testGetReadyToExecuteMissions() {
		List<Mission> ready = Board.getReadyToExecuteMissions();
		assertEquals(ready.size(), 1);
		assertEquals(ready.get(0), this.readyMission);
		this.readyMission.setTimeLeft(0);
		ready = Board.getReadyToExecuteMissions();
		assertEquals(ready.size(), 1);
		assertEquals(ready.get(0), this.firstDepends);
		this.firstDepends.setTimeLeft(0);
		ready = Board.getReadyToExecuteMissions();
		assertEquals(ready.size(), 1);
		assertEquals(ready.get(0), this.secondDepends);
		this.secondDepends.setTimeLeft(0);
		ready = Board.getReadyToExecuteMissions();
		assertEquals(ready.size(), 0);
	}

	/**
	 * Test method for {@link spl.army.Board#getMissionListFromString(java.lang.String)}.
	 */
	@Test
	public void testGetMissionListFromString() {
		String missionListStr = "KillBinLaden1,KillBinLaden2,KillBinLaden3";
		List<Mission> missions = Board.getMissionListFromString(missionListStr);
		assertEquals(missions.size(), 3);
		assertEquals(missions.get(0), this.readyMission);
		assertEquals(missions.get(1), this.firstDepends);
		assertEquals(missions.get(2), this.secondDepends);
		
		missionListStr = "KillBinLaden1  , KillBinLaden2,   KillBinLaden3";
		missions = Board.getMissionListFromString(missionListStr);
		assertEquals(missions.size(), 3);
		assertEquals(missions.get(0), this.readyMission);
		assertEquals(missions.get(1), this.firstDepends);
		assertEquals(missions.get(2), this.secondDepends);		
	}

	/**
	 * Test method for {@link spl.army.Board#addMission(java.lang.String, java.lang.String, int, java.lang.String, java.util.List)}.
	 */
	@Test
	public void testAddMissionStringStringIntStringListOfItemRequired() {
		Board.addMission("newMission", "sniper", 5, "KillBinLaden1", this.noItemsRequireds );
		Mission newMission = Board.getMissionByName("newMission");
		assertEquals(newMission.getMissionName(), "newMission");
		assertEquals(newMission.getRequiredSkill(), "sniper");
		assertEquals(newMission.getTimeLeft(), 5);
		assertEquals(newMission.getPreReqMissions().size(), 1);
		assertEquals(newMission.getPreReqMissions().get(0), this.readyMission);
		assertEquals(newMission.getItemsRequired().size(), 0);
	}

	/**
	 * Test method for {@link spl.army.Board#addMission(java.lang.String, java.lang.String, int, java.util.List, java.util.List)}.
	 */
	@Test
	public void testAddMissionStringStringIntListOfMissionListOfItemRequired() {
		ArrayList<Mission> missionReq = new ArrayList<Mission>();
		missionReq.add(this.readyMission);
		missionReq.add(this.firstDepends);
		missionReq.add(this.secondDepends);
		Board.addMission("newMission", "sniper", 5, missionReq, this.noItemsRequireds );
		Mission newMission = Board.getMissionByName("newMission");
		assertEquals(newMission.getMissionName(), "newMission");
		assertEquals(newMission.getRequiredSkill(), "sniper");
		assertEquals(newMission.getTimeLeft(), 5);
		assertEquals(newMission.getPreReqMissions().size(), 3);
		assertEquals(newMission.getPreReqMissions().get(0), this.readyMission);
		assertEquals(newMission.getItemsRequired().size(), 0);
	}

	/**
	 * Test method for {@link spl.army.Board#addMission(spl.army.Mission)}.
	 */
	@Test
	public void testAddMissionMission() {
		ArrayList<Mission> missionReq = new ArrayList<Mission>();
		missionReq.add(this.readyMission);
		missionReq.add(this.firstDepends);
		missionReq.add(this.secondDepends);
		Mission newMission = new Mission("newMission", "sniper", 5, this.noItemsRequireds, missionReq);
		Board.addMission(newMission);
		Mission gotMission = Board.getMissionByName("newMission");
		assertEquals(gotMission.getMissionName(), "newMission");
		assertEquals(gotMission.getRequiredSkill(), "sniper");
		assertEquals(gotMission.getTimeLeft(), 5);
		assertEquals(gotMission.getPreReqMissions(), missionReq);
		assertEquals(gotMission.getPreReqMissions().get(0), this.readyMission);
		assertEquals(gotMission.getItemsRequired().size(), 0);
	}

	/**
	 * Test method for {@link spl.army.Board#getMissions()}.
	 */
	@Test
	public void testGetMissions() {
		List<Mission> missions = Board.getMissions();
		assertEquals(missions.size(), 3);
		assertEquals(missions.get(0), this.readyMission);
		assertEquals(missions.get(1), this.firstDepends);
		assertEquals(missions.get(2), this.secondDepends);
	}

	/**
	 * Test method for {@link spl.army.Board#getMissions()}.
	 */
	@Test
	public void testIsAllDone() {
		List<Mission> missions = Board.getMissions();
		assertFalse(Board.isAllDone());
		Iterator<Mission> it = missions.iterator();
		while(it.hasNext())
		{
			it.next().setTimeLeft(0);
		}
		assertTrue(Board.isAllDone());
	}	
	
}
